#
# Hill, Seth J. and Chris Tausanovitch. "A Disconnect in Representation? Comparison of Trends in Congressional and Public Polarization."
#

library(rjags)

load("allyearsMultinomialFull.RData")

adapt <- 500
iterations <- 25000
thin <- 10
  
starting=list(x=startvalues+rnorm(length(startvalues)))

unrolly <- function(data) {
   m <- dim(data$y)[2]; n <- dim(data$y)[1]
   yy <- na.omit(cbind(as.vector(data$y),expand.grid(1:n,1:m)))
   list(yy=yy[,1],nidx=yy[,2],midx=yy[,3],
        lengthy=dim(yy)[1],n=n,m=m,y=data$y,
        codes=data$codes,max.K=max(data$K),
        K=data$K,yearparty=data$yearparty,nyearparty=data$nyearparty)
}
jdata <- unrolly(jdata)

placeholders <- list(p=array(999,dim=c(jdata$n,jdata$m,max(jdata$K))),prob=array(999,dim=c(jdata$n,jdata$m,max(jdata$K))))

print("Preparing the model...")
system.time(
  model <- jags.model(file="multinomial.jags",inits=starting,n.adapt=adapt,data=jdata)
)
print("Running the model...")
system.time(
output <- coda.samples(model, variable.names=c("x","beta","alpha"),
                    n.iter=iterations,thin=thin)
          )
save("output","iterations","thin","jdata",file="jagsoutFull25kchain1.RData")
